package com.snap.vseries.analyze;

import com.snap.vseries.bean.ChannelBean;

import org.apache.commons.math3.stat.StatUtils;

import java.util.List;

public class ThresholdStd10 {

    public static void doAnalyze(List<ChannelBean> channelBeans, AlgoParams algoParams) {
        double defThreshold = algoParams.getDefThreshold();
        double minThreshold = algoParams.getMinThreshold();
        for (int i = 0; i < channelBeans.size(); i++) {
            float[] fit = channelBeans.get(i).getFit();
            int start = channelBeans.get(i).getBlStart() - 1;
            int len = channelBeans.get(i).getBlStop() - start;
            if (fit == null || fit.length == 0
                    || start < 0 || len < 2 || start + len > fit.length) {
                channelBeans.get(i).setThreshold((float) defThreshold);
                continue;
            }

            double[] sub = new double[len];
            for (int j = 0; j < len; j++) {
                sub[j] = fit[start + j];
            }
            double van = StatUtils.variance(sub);
            double sd10 = 10 * Math.sqrt(van) + 1;
            System.out.println(sd10);
            float threshold = (float) Math.max(sd10, minThreshold);
            channelBeans.get(i).setThreshold(threshold);
        }
    }
}
